package com.xiaoshi.oauth.impl;

import com.xiaoshi.common.MyResUser;
import com.xiaoshi.system.entity.SysUser;
import com.xiaoshi.enums.JsonReturnEnums;
import com.xiaoshi.exception.PlatformException;
import com.xiaoshi.interfaces.Constants;
import com.xiaoshi.system.service.ISysUserService;
import com.xiaoshi.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 默认的登录方法
 */
@Service
public class UserDetailLoginServiceImpl implements UserDetailsService {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private AuthenticationManager authenticationManager;


    @Autowired
    private RedisUtils redisUtils;


    /**
     * 登入的逻辑
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysUser sysUser = sysUserService.findByName(username);
        if (sysUser != null) {
            if(sysUser.STATUS_LOCK.equals(sysUser.getStatus())){
                throw new PlatformException(JsonReturnEnums.USER_ISLOCK);
            }
            boolean notLocked = false;
            if (StringUtils.equals(sysUser.STATUS_VALID, sysUser.getStatus())) notLocked = true;

            String userPermissions = sysUserService.findUserPermissions(sysUser.getUsername());// 菜单权限

            MyResUser user = new MyResUser(sysUser.getUsername(), sysUser.getPassword(), true, true, true, notLocked,
                    AuthorityUtils.commaSeparatedStringToAuthorityList(userPermissions));

            BeanUtils.copyProperties(sysUser, user);

            return user;

        } else {
            throw new PlatformException(JsonReturnEnums.ACCOUNT_NOT_FIND);
        }
    }


    /**
     * 登录
     *
     * @param parameters
     * @return
     */
    public MyResUser userLogin(Map<String, String> parameters) {
        String grantType = parameters.get(Constants.myConstans.GRANT_TYPE);
        String username = parameters.get(Constants.myConstans.USERNAME);
        String password = parameters.get(Constants.myConstans.PASSWORD);
        MyResUser myResUser = (MyResUser) loadUserByUsername(username);

        Authentication userAuth = new UsernamePasswordAuthenticationToken(username, password, myResUser.getAuthorities());
        ((AbstractAuthenticationToken) userAuth).setDetails(parameters);
        userAuth = authenticationManager.authenticate(userAuth);
        return (MyResUser) userAuth.getPrincipal();
    }





}
